const app = getApp();
const numeral = require('../../../vendor/numeral/numeral.min')
const util = require('../../../utils/util')
Page({
    data: {},
    onLoad(options) {
        this.setData(options)
        app.http.get("order/status-dict").then(res => {
            let statusObj = {}
            Object.keys(res.data).forEach(k => {
                statusObj[res.data[k].code] = res.data[k].name
            })
            this.setData({
                statusObj
            })
        })
    },

    onShow() {
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '加载中..',
        })
        app.http.get("order/detail", {
            data: { id: this.data.id },
        }).then(result => {
            const order = result.data.order
            order.goodsOrderItems.money = numeral(order.goodsOrderItems.price / 100).format("0,0.00")
            order.total_money = numeral(order.total_cost / 100).format('0,0.00')
            order.act_money = numeral((order.express_cost + order.total_cost - order.express_cost) / 100).format('0,0.00')
            order.express_money = numeral(order.express_cost / 100).format("0,0.00")
            order.discount_money = numeral(order.discount_cost / 100).format("0,0.00")
            order.time = util.formatTime(new Date(order.created_at * 1000))
            console.log(result.data.order)
            this.setData(Object.assign({ loading: false, }, result.data));
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            app.wxToast.error(err.message)
        })

    },

    onPullDownRefresh() {
        this.onShow()
    },


    onShareAppMessage() {
        if (this.data.order.type == 1) {
            return {
                title: '我购买了' + this.data.goods[0].data.goods_name,
                path: '/pages/goods/detail/detail?id=' + this.data.goods[0].data.goods_id
            }
        }
        return {
            title: '我购买了' + this.data.goods[0].data.goods_name,
            path: '/pages/goods/detail/detail?id=' + this.data.goods[0].data.goods_id
        }
    },


    pay() {
        app.global_data.pay_info = {
            image: this.data.goods[0].data.goods_image,
            is_complete: false,
            title: this.data.goods[0].data.goods_name,
            price: this.data.order.price,
            disable_address: true,
            disable_num: true,
            address_id: this.data.address.id,
            data: { id: this.data.order.id },
            pay_url: app.config.pay.re_pay_url
        }

        wx.redirectTo({
            url: '../../pay/pay',
        });
    },

    call(evt) {
        wx.makePhoneCall({
            phoneNumber: evt.currentTarget.dataset.number,
            success(res) {
            }
        })
    },

    openMap() {
        const { lat, lng, name, address } = this.data.childStore
        wx.openLocation({
            latitude: parseFloat(lat),
            longitude: parseFloat(lng),
            name,
            address
        })
    },


    pay() {
        wx.showLoading({
            title: '支付中...',
        })
        app.http.get("member/pay-again", {
            data: { id: this.data.order.id },
        }).then(res => {

            wx.hideLoading()
            wx.requestPayment(Object.assign({
                success: res => {
                    this.onPullDownRefresh()
                },
                fail: err => {
                    app.wxToast.warn("支付失败，请重新支付")
                }
            }, res.data.js))
        }).catch(err => {
            app.wxToast.warn(err.message)
        })
    },
    tgDetail() {
        if (this.data.order.type == 2) {
            wx.navigateTo({
                url: '/pages/pt/ok/ok?id=' + this.data.order.id,
            })
        } else {
            const ps = getCurrentPages()
            if (ps[ps.length - 2].route == 'pages/goods/lucky') {
                wx.navigateBack({

                })
            } else {
                wx.navigateTo({
                    url: '/pages/goods/lucky?id=' + this.data.order.luckyInfo.id,
                })
            }
        }
    }
})